package com.tedu.redisdemo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.io.Serializable;

/*
* Spring的配置类，用于配置Redis
* 在里面维护一个RedisTemple，用于Spring IOC容器帮我们在需要的地方
* 自动注入该类
* */
@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Serializable> initRedisTemplate(
            RedisConnectionFactory redisConnectionFactory){
        //初始化Redis的模版类
        RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
        //为模版类设置链接工厂
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        //key序列化为字符串形式
        redisTemplate.setKeySerializer(RedisSerializer.string());
        redisTemplate.setValueSerializer(RedisSerializer.json());
        return redisTemplate;
    }
}























